<?php

namespace app\common\service;

use app\common\service\Base;

class OrderMaterialItem extends Base
{

    /**
     * 根据订单物料映射ID获取列表
     *
     * @param integer $orderMaterialId order_material表ID mid
     * @param string $field 需要显示的字段
     * @param integer $isReturn 是否退料（0不含退料， 1退料， null全部）
     * @return array
     * @author KING
     * @date 2020年7月13日
     *
     */
    public function getListByOrderMaterialId($orderMaterialId, $field = '*', $isReturn = 0)
    {
        if (empty($orderMaterialId)) return [];
        $where = [
            ['omi.order_material_id', '=', $orderMaterialId],
            ['omi.flag_del', '=', 0],
        ];
        if ($isReturn !== null) $where[] = ['omi.is_return', '=', $isReturn];
        return db('order_material_item omi')
            ->field('omi.*,om.tiaoshu')
            ->where($where)
            ->join('order_material om', "omi.order_material_id=om.mid", 'left')
            ->order('omi.cloth_num asc,omi.id asc')
            ->select();
    }

    /**
     * 根据ID获取记录信息
     *
     * @param integer $id
     * @param string $field
     * @return array
     * @author KING
     * @date 2020年7月9日
     *
     */
    public function getInfoById($id, $field = '*')
    {
        if (empty($id)) return [];
        return db('order_material_item')->where('id', $id)->field($field)->findOrEmpty();
    }

    /**
     * 获取最大布号数
     *
     * @param integer $orderId
     * @param integer $materialId
     * @return integer
     * @author KING
     * @date 2020年5月10日
     *
     */
    public function getMaxClothNum($orderId, $materialId,$stock_m_type=0,$mid=0)
    {
        if ($stock_m_type == 1) {
            $where = [
                ['order_material_id', '=', $mid],
            ];
            return db('order_material_item')->where($where)->order('cloth_num desc')->value('cloth_num', 0);
        }
        if (empty($orderId) || empty($materialId)) return 0;
        $where = [
            ['order_id', '=', $orderId],
            ['material_id', '=', $materialId],
        ];
        return db('order_material_item')->where($where)->order('cloth_num desc')->value('cloth_num', 0);
    }

    /**
     * 检测物料是否都已验布缩水完成
     *
     * @param integer $orderId
     * @param integer $materialId
     * @author King
     * @date 2020年8月27日
     *
     */
    public function checkCheckclothShrinkOver($orderId, $materialId)
    {
        $where = [
            ['order_id', '=', $orderId],
            ['material_id', '=', $materialId],
            ['is_shrink', '=', 0],
            ['need_shrink', '=', 1],
        ];
        $countShrink = db('order_material_item')->where($where)->count();

        $where = [
            ['order_id', '=', $orderId],
            ['material_id', '=', $materialId],
            ['is_checkcloth', '=', 0],
            ['need_checkcloth', '=', 1],
        ];
        $countCheckcloth = db('order_material_item')->where($where)->count();

        if ($countShrink || $countCheckcloth) return false;
        return true;
    }

    /**
     * 获取布号列表
     *
     * @param integer $orderId
     * @param integer $materialId
     * @param integer $type 方式（0全部，1未验布无退料，2未缩水无退料， 3未验布且未缩水无退料）
     * @return array
     * @author KING
     * @date 2020年5月11日
     *
     */
    public function getClothNumList($orderId, $materialId, $type = 0)
    {
        if (empty($orderId) || empty($materialId)) return [];
        $where = [
            ['order_id', '=', $orderId],
            ['material_id', '=', $materialId],
        ];
        if ($type == 1) {    // 未验布，无退料
            $where[] = ['is_checkcloth', '=', 0];
            $where[] = ['is_return', '=', 0];
        } elseif ($type == 2) {    // 未缩水，无退料
            $where[] = ['is_shrink', '=', 0];
            $where[] = ['is_return', '=', 0];
        } elseif ($type == 3) { // 未验布且未缩水，无退料
            $where[] = ['is_checkcloth', '=', 0];
            $where[] = ['is_shrink', '=', 0];
            $where[] = ['is_return', '=', 0];
        }
        return db('order_material_item')->where($where)->field("id,cloth_num,is_checkcloth,is_shrink,is_return")->select();
    }

    /**
     * 更新布号米长
     *
     * @param integer $orderId
     * @param integer $materialId
     * @author King
     * @date 2020年9月7日
     *
     */
    public function updateLength($orderId, $materialId, $order_material_id = 0)
    {
        if ($order_material_id > 0) {
            $where = [
                ['order_material_id', '=', $order_material_id],
            ];
            $list = db('order_material_item')->where($where)->where("checkcloth_id > 0 OR shrink_id > 0")->field('id,checkcloth_id,shrink_id,length')->select();
            foreach ($list as $k => $v) {
                $length = 0;
                if ($v['shrink_id'] > 0) {
                    $length = db('checkcloth_shrink')->where('id', $v['shrink_id'])->where('is_return', 0)->value('length', 0);
                }
                if (!$length && $v['checkcloth_id'] > 0) {
                    $length = db('checkcloth_shrink')->where('id', $v['checkcloth_id'])->where('is_return', 0)->value('length', 0);
                }
                if ($length != $v['length']) {
                    db('order_material_item')->where('id', $v['id'])->update(['length' => $length, 'update_time' => time()]);
                }
            }
            return true;
        }
        $where = [
            ['order_id', '=', $orderId],
            ['material_id', '=', $materialId],
        ];
        $list = db('order_material_item')->where($where)->where("checkcloth_id > 0 OR shrink_id > 0")->field('id,checkcloth_id,shrink_id,length')->select();
        foreach ($list as $k => $v) {
            $length = 0;
            if ($v['shrink_id'] > 0) {
                $length = db('checkcloth_shrink')->where('id', $v['shrink_id'])->where('is_return', 0)->value('length', 0);
            }
            if (!$length && $v['checkcloth_id'] > 0) {
                $length = db('checkcloth_shrink')->where('id', $v['checkcloth_id'])->where('is_return', 0)->value('length', 0);
            }
            if ($length != $v['length']) {
                db('order_material_item')->where('id', $v['id'])->update(['length' => $length, 'update_time' => time()]);
            }
        }
        return true;
    }

    /**
     * 获取条目ID
     *
     * @param integer $orderId
     * @param integer $materialId
     * @param integer $clothNum
     * @author KING
     * @date 2020年5月11日
     *
     */
    public function getIdByClothNum($orderId, $materialId, $clothNum, $order_material_id = 0)
    {
        if ($order_material_id > 0) {
            $where = [
                ['order_material_id', '=', $order_material_id],
                ['cloth_num', '=', $clothNum],
            ];
            return db('order_material_item')->where($where)->value('id', 0);
        }
        if (empty($orderId) || empty($materialId)) return 0;
        $where = [
            ['order_id', '=', $orderId],
            ['material_id', '=', $materialId],
            ['cloth_num', '=', $clothNum],
        ];
        return db('order_material_item')->where($where)->value('id', 0);
    }

    /**
     * 面料计划数据
     * @param $orderMaterialId
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/1
     */
    public function material_cloth_plan($orderMaterialId = 0, $order_id = 0)
    {
        $res = [];
        $where = [];
        $where[] = ['omi.flag_del', '=', 0];
        if ($orderMaterialId > 0) {
            $where[] = ['omi.order_material_id', '=', $orderMaterialId];
        }
        if ($order_id > 0) {
            $where[] = ['omi.order_id', '=', $order_id];
        }
        $items = db('order_material_item omi')
            ->field('omi.*,om.tiaoshu')
            ->where($where)
            ->join('order_material om', "omi.order_material_id=om.mid", 'left')
            ->order('omi.cloth_num asc,omi.id asc')
            ->select();
        if (count($items) > 0) {
            $sh_list = db('checkcloth_shrink')
                ->where([
                    ['order_id', '=', $items[0]['order_id']],
                ])
                ->order('type asc,cloth_num asc')
                ->select();
            foreach ($items as $k => $v) {
                $temp = $v;
                $temp['rejects_length'] = 0;
                foreach ($sh_list as $k2 => $v2) {
                    if ($v['cloth_num'] == $v2['cloth_num'] && $v2['is_rejects'] == 1 && $temp['rejects_length'] == '') {
                        $temp['rejects_length'] = $v2['rejects_length'];
                        break;
                    }
                }
                $res[] = $temp;
            }
        }
        return $res;
    }
}